//
//  ShareTool.m
//  YunDongGym
//
//  Created by lqq on 2018/6/8.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "ShareTool.h"
#import <UMShare/UMShare.h>
#import <SDWebImage/SDWebImageDownloader.h>
#import "UIImage+Extension.h"
@implementation ShareTool


/**
 分享
 
 @param webUrl 网址
 @param title 标题
 @param desc 摘要
 @param thumImage 缩略图或者（缩略图地址）
 */
+ (void)shareWebUrlToPlatorm:(SharePlatformType)platformType webUrl:(NSString *)webUrl title:(NSString *)title desc:(NSString *)desc thumImage:(id)thumImage currentController:(id)currentViewController
{
    UMSocialPlatformType umShareType = UMSocialPlatformType_Sina;//默认为新浪
    switch (platformType) {
        case SharePlatformType_WechatSession:
        {
            BOOL isSupportWeiXin = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession];
            if (!isSupportWeiXin) {
                [AV showRemind:@"缺少相关应用"];
                
                return;
            }
            umShareType = UMSocialPlatformType_WechatSession;
        }
            break;
        case SharePlatformType_Sina:
            umShareType = UMSocialPlatformType_Sina;
            break;
        case SharePlatformType_WechatTimeLine:
        {
            BOOL isSupportWeiXin = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession];
            if (!isSupportWeiXin) {
                [AV showRemind:@"缺少相关应用"];
                
                return;
            }
            umShareType = UMSocialPlatformType_WechatTimeLine;
        }
            break;
            
        default:
            break;
    }
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建图片内容对象
    UMShareWebpageObject *shareObject = [[UMShareWebpageObject alloc] init];
    shareObject.webpageUrl = webUrl;
    shareObject.title = title;
    shareObject.descr = desc;
    //如果有缩略图，则设置缩略图
    
    void(^successBlock)() = ^{
        [AV showSuccess:@"分享成功"];
    };
    
    
    if ([thumImage isKindOfClass:[NSString class]]) {
        [AV showLoading:@"请稍候"];
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumImage] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                
                //回到主线程
                shareObject.thumbImage = image ? image : [UIImage imageNamed:@"ic_launcher_APP"];
                
                
                //分享消息对象设置分享内容对象
                messageObject.shareObject = shareObject;
                //调用分享接口
                
                [[UMSocialManager defaultManager] shareToPlatform:umShareType messageObject:messageObject currentViewController:currentViewController completion:^(id data, NSError *error) {
                    if (!error) {
                        successBlock();
                    }else {
                        NSString *errorStr = SAFE_VALUE_FOR_KEY(error.userInfo, @"message");
                        [AV showRemind:errorStr];
                    }
                }];
                
                [AV hideen];
                
            });
        }];
        
    } else if ([thumImage isKindOfClass:[UIImage class]] || [thumImage isKindOfClass:[NSString class]]) {
        shareObject.thumbImage = thumImage;
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        //调用分享接口
        
        [[UMSocialManager defaultManager] shareToPlatform:umShareType messageObject:messageObject currentViewController:currentViewController completion:^(id data, NSError *error) {
            if (!error) {
                successBlock();
            }else {
                NSString *errorStr = SAFE_VALUE_FOR_KEY(error.userInfo, @"message");
                [AV showRemind:errorStr];
            }
        }];
        
    } else {
        shareObject.thumbImage = [UIImage imageNamed:@"ic_launcher_APP"];
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        //调用分享接口
        
        [[UMSocialManager defaultManager] shareToPlatform:umShareType messageObject:messageObject currentViewController:currentViewController completion:^(id data, NSError *error) {
            if (!error) {
                successBlock();
            }else {
                NSString *errorStr = SAFE_VALUE_FOR_KEY(error.userInfo, @"message");
                [AV showRemind:errorStr];
            }
        }];
    }
}



/**
 分享微信小程序
 
 @param webUrl 兼容微信低版本网页地址
 @param userName 小程序username
 @param path 小程序页面路径
 @param title 小程序标题
 @param desc 小程序内容描述
 @param hdImage 小程序高清图
 @param currentViewController 当前控制器
 */
+ (void)shareMiniProgramWithWebUrl:(NSString *)webUrl userName:(NSString *)userName path:(NSString *)path title:(NSString *)title desc:(NSString *)desc hdImage:(id)hdImage currentController:(id)currentViewController {
    BOOL isSupportWeiXin = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession];
//    BOOL isSupportQQ = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];
    if (!isSupportWeiXin) {
        [AV showRemind:@"缺少相关应用"];
        
        return;
    }
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    void(^successBlock)(id data) = ^(id data){
        [AV showSuccess:@"分享成功"];
    };
    
    if ([hdImage isKindOfClass:[NSString class]]) {
        [AV showLoading:@"请稍候"];
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:hdImage] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:title.length > 0 ? title : @"云动管理" descr:desc.length > 0 ? desc : @"把规律当成一种生活习惯，一切会变得更好" thumImage:image];
                shareObject.webpageUrl = webUrl;
                shareObject.userName = userName;
                shareObject.path = path;
                messageObject.shareObject = shareObject;
                shareObject.hdImageData = [UIImage compressImage:image toByte:100*1024];
                shareObject.miniProgramType = UShareWXMiniProgramTypeRelease; // 可选体验版和开发板
                
                //调用分享接口
                [AV hideen];
                [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:currentViewController completion:^(id data, NSError *error) {
                    if (!error) {
                        successBlock(data);
                        
                    } else {
                        NSString *errorStr = SAFE_VALUE_FOR_KEY(error.userInfo, @"message");
                        [AV showRemind:errorStr];
                    }
                }];
                
            });
        }];
        
    } else {
        UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:title.length > 0 ? title : @"云动管理" descr:desc.length > 0 ? desc : @"把规律当成一种生活习惯，一切会变得更好" thumImage:hdImage];
        shareObject.webpageUrl = webUrl;
        shareObject.userName = userName;
        shareObject.path = path;
        messageObject.shareObject = shareObject;
        shareObject.hdImageData = [UIImage compressImage:hdImage toByte:100*1024];
        shareObject.miniProgramType = UShareWXMiniProgramTypeRelease; // 可选体验版和开发板
        
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:currentViewController completion:^(id data, NSError *error) {
            if (!error) {
                successBlock(data);
                
            } else {
                NSString *errorStr = SAFE_VALUE_FOR_KEY(error.userInfo, @"message");
                [AV showRemind:errorStr];
            }
        }];
        
    }
}

/**
 分享图片
 */
+ (void)shareImageToPlatform:(SharePlatformType)platform WithImage:(id)shareImage
{
    BOOL isSupportWeiXin = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession];
    if (!isSupportWeiXin) {
        [AV showRemind:@"缺少相关应用"];
        return;
    }
    UMSocialPlatformType UMPlatform = UMSocialPlatformType_Sina;
    if (platform == SharePlatformType_WechatSession) {
        UMPlatform = UMSocialPlatformType_WechatSession;
    } else if (platform == SharePlatformType_WechatTimeLine) {
        UMPlatform = UMSocialPlatformType_WechatTimeLine;
    }
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareImageObject *shareObj = [[UMShareImageObject alloc] init];
    shareObj.shareImage = shareImage;
    messageObject.shareObject = shareObj;
    [[UMSocialManager defaultManager] shareToPlatform:UMPlatform messageObject:messageObject currentViewController:nil completion:^(id result, NSError *error) {
        if(error) {
            NSString *errorStr = SAFE_VALUE_FOR_KEY(error.userInfo, @"message");
            [AV showRemind:errorStr];
        } else {
            [AV showSuccess:@"分享成功"];
        }
    }];
}
@end
